package com.lanou.renrenxin.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.renrenxin.R;
import com.lanou.renrenxin.mybomb.Invitation;

import java.util.List;

/**
 * Created by zouhui on 16/3/23.
 */
public class InvitationAdapter extends RecyclerView.Adapter<InvitationAdapter.InvitationViewHolder> {
    private Context context;
    private List<Invitation> data;
    private String username;
    private InvitationOnItemClickListener onItemClickListener;

    public void setOnItemClickListener(InvitationOnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public InvitationAdapter(Context context,List<Invitation> data) {
        this.context = context;
        this.username = username;
        this.data = data;
    }


    @Override
    public InvitationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recycle_addcontact,parent,false);
        InvitationViewHolder holder = new InvitationViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final InvitationViewHolder holder, int position) {
        holder.pos = position;
        final Invitation invitation = data.get(position);
        holder.headImg.setImageResource(R.mipmap.headimg);
        holder.userNameTv.setText(invitation.getFriendUsername());
        if (!invitation.getIsAgree()) {
            holder.addBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onButtonClick(invitation.getFriendUsername());
                    holder.addBtn.setText("已同意");

                }
            });
        }else {
            holder.addBtn.setText("已经添加");
            holder.addBtn.setEnabled(false);
        }
    }

    @Override
    public int getItemCount() {
        return data!=null?data.size():0;
    }

    public interface InvitationOnItemClickListener{
        void onButtonClick(String username);
    }

    class InvitationViewHolder extends RecyclerView.ViewHolder {
        private ImageView headImg;
        private TextView userNameTv;
        private Button addBtn;
        private int pos;
        public InvitationViewHolder(View itemView) {
            super(itemView);
            headImg = (ImageView) itemView.findViewById(R.id.iv_item_recycle_addcontact_headImg);
            userNameTv = (TextView) itemView.findViewById(R.id.tv_item_recycle_addcontact_name);
            addBtn = (Button) itemView.findViewById(R.id.btn_item_recycle_addcontact_add);
            addBtn.setText("同意");
        }
    }
}
